package com.kaiwukj.android.ufamily.mvp.ui.page.home.message;

import android.app.Application;

import androidx.annotation.NonNull;

import com.kaiwukj.android.mcas.di.scope.ActivityScope;
import com.kaiwukj.android.mcas.http.imageloader.ImageLoader;
import com.kaiwukj.android.mcas.integration.AppManager;
import com.kaiwukj.android.mcas.mvp.BasePresenter;
import com.kaiwukj.android.mcas.utils.RxLifecycleUtils;
import com.kaiwukj.android.ufamily.mvp.contract.ChatContract;
import com.kaiwukj.android.ufamily.mvp.http.callback.ApiCallBack;
import com.kaiwukj.android.ufamily.mvp.http.entity.resultbak.CommunityGroupResult;
import com.kaiwukj.android.ufamily.mvp.http.utils.RxUtils;

import java.util.List;

import javax.inject.Inject;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;
import me.jessyan.rxerrorhandler.core.RxErrorHandler;


@ActivityScope
public class ChatPresenter extends BasePresenter<ChatContract.Model, ChatContract.View> {
    @Inject
    RxErrorHandler mErrorHandler;
    @Inject
    Application mApplication;
    @Inject
    ImageLoader mImageLoader;
    @Inject
    AppManager mAppManager;

    @Inject
    public ChatPresenter(ChatContract.Model model, ChatContract.View rootView) {
        super(model, rootView);
    }

    /**
     * 获取聊天组列表
     */
    public void requestGroupList() {
        mModel.requestGroupList()
                .subscribeOn(Schedulers.io())
                .compose(RxLifecycleUtils.bindToLifecycle(mRootView))
                .unsubscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .compose(RxUtils.handleResult())
                .subscribe(new ApiCallBack<List<CommunityGroupResult>>(mErrorHandler, mRootView) {

                    @Override
                    public void onSuccess(@NonNull List<CommunityGroupResult> result) {
                        mRootView.onGetNoticeList(result);
                    }

                   /* @Override
                    public void onNext(BaseListResponse<CommunityGroupResult> result) {
                        if (result.getCode().equals(Api.RequestSuccess)) {
                            mRootView.onGetNoticeList(result.getResult());
                        }
                    }*/
                });
    }

    @Override
    public void detachView() {
        super.detachView();
        this.mErrorHandler = null;
        this.mAppManager = null;
        this.mImageLoader = null;
        this.mApplication = null;
    }
}
